package com.angular.useradmin.user.controller;


import com.angular.useradmin.user.common.result.R;
import com.angular.useradmin.user.entity.New;
import com.angular.useradmin.user.entity.vo.FiledArrays;
import com.angular.useradmin.user.service.NewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 许根银
 * @since 2023-01-05
 */
@RestController
@RequestMapping("/user/new")
@Api(description = "userNew")
@CrossOrigin
public class NewController {
    private final NewService newService;

    public NewController(NewService newService) {
        this.newService = newService;
    }

    @GetMapping("/getall")
    @ApiOperation("test")
    public R saveNew() {
        List<New> list = newService.list(null);
        return R.ok().data("list", list);
    }


    @PostMapping("/copy")
    @ApiOperation("整表复制")
    public R save() {
        boolean result = newService.copyTable();
        return R.ok().message(String.valueOf(result));
    }


    @GetMapping("/getdata")
    @ApiOperation("查询结果")
    public R getQueryData() {
        List<Map<String, Object>> list = newService.getQueryData();
        return R.ok().data("list", list);
    }

    @GetMapping("/alltable")
    @ApiOperation("获取到数据库下所有表名")
    public R getAllTableName() {
        String[] tableName = newService.getAllTableName();
        return R.ok().data("tableName", tableName);
    }

    @GetMapping("/allfield/{tableName}")
    @ApiOperation(" 获取到表下所有字段")
    public R getAllFieldName(@PathVariable String tableName) {
        String[] allFieldName = newService.getAllFieldName(tableName);
        return R.ok().data("tableName", allFieldName);
    }

    @GetMapping("/allvalue/{tableName}/{current}/{limit}")
    @ApiOperation(" 获取到字段下所有值")
    public R getAllValue(@PathVariable String tableName,@PathVariable int current,@PathVariable int limit) {
        Map map= newService.getAllValue(tableName, current, limit);
        return R.ok().data(map).data("field",getAllFieldName(tableName));
    }

    /**
     * @param filedArrays A表传过来的字段名数组+ B表传来的字段名数组 A表名+B表名
     * @return
     */
    @PostMapping("/translate")
    @ApiOperation("表部分数据转移")
    public R save2(@RequestBody @NotNull FiledArrays filedArrays) {
//        filedArrays.setTableMaster(new String[]{"id","name","addr","sex"});
//        filedArrays.setTableNew(new String[]{"name","addr","sex"});
//        filedArrays.setNewTableName("user_new");
//        filedArrays.setQueryTableName("user_message");
        boolean result = newService.translate(filedArrays);
        if (result == false) {
            return R.ok().message("G了");
        } else
            return R.ok().data("result", true);
    }

}

